"use strict";

/**
 * 学生相关接口调用
 */
import Ajax from "../libs/ajax";
import Constant from "../libs/constants";

export function addStudent(data) {
    return Ajax.ajax({
        url: "/api/user/add_student",
        data: data,
        method: "post",
        timeout:20*1000,
    }).then(res => res.data);
}

export function updateStudent(data) {
    return Ajax.ajax({
        url: "/api/user/update_student",
        data: data,
        method: "post",
        timeout:20*1000,
    }).then(res => res.data);
}

export function batchUpdateStudent(data) {
    return Ajax.ajax({
        url: "/api/user/batch_update_library",
        data: data,
        method: "post",
        timeout:20*1000,
    }).then(res => res.data);
}

export function deleteStudent(data) {
    return Ajax.ajax({
        url: "/api/user/delete_student",
        data: data,
        method: "post",
        timeout:20*1000,
    }).then(res => res.data);
}

export function getStudentData(page_num, page_size, user_name) {
    const baseUrl = "/api/user/query?";
    if (page_num === undefined || page_size === undefined) {
        return new Promise((resolve, reject) => {
            resolve({ code: 2011, msg: "学生查询参数缺失" });
        });
    }
    let queryArg = [
        `page_num=${page_num}`,
        `page_size=${page_size}`,
        'type=student'
    ];

    if (user_name !== undefined) {
        queryArg.push(`user_name=${user_name}`);
    }

    return Ajax.ajax({
        url: baseUrl + queryArg.join("&"),
        method: "get",
        timeout: 20 * 1000,
    }).then(res => res.data);
}



export function delFeature(data) {
    return Ajax.ajax({
        url: "/api/user/delete_feature",
        data: data,
        method: "post"
    }).then(res => res.data);
}

export function addOrUpdateGuest(url, data) {
    return Ajax.ajax({
        url: "/api/user/" + url,
        method: "post",
        data: data
    }).then(res => res.data);
}

//学生批量上传
export function uploadStudentData(data) {
    return Ajax.ajax({
        url: '/api/user/upload_student',
        method: 'post',
        data: data,
        timeout:30*1000,
    }).then(res => res.data)
}

// 获取学生信息
export function getStudentById(_id){
    if(!_id){
        return new Promise((resolve, reject) => {
            resolve({ code: 2011, msg: "参数缺失" });
        });
    }
    return Ajax.ajax({
        url: `/api/user/get_student_by_id?id=${_id}`,
        method: 'get',
    }).then(res => res.data);
}

// 保存特征值
export function saveStudentFeature(data){
    if(!data.id){
        return new Promise((resolve, reject) => {
            resolve({ code: 2011, msg: "参数缺失" });
        });
    }
    return Ajax.ajax({
        url: `/api/faceFeature/save`,
        method: 'post',
        data,
        timeout:20*1000,
    }).then(res => res.data);
}

// 批量保存特征值
export function batchSaveFeature(data){
    if(!data.ids){
        return new Promise((resolve, reject) => {
            resolve({ code: 2011, msg: "参数缺失" });
        });
    }
    return Ajax.ajax({
        url: `/api/faceFeature/batch_save`,
        method: 'post',
        data,
        timeout:20*1000,
    }).then(res => res.data);
}

// 删除特征值
export function deleteStudentFeature(data){
    if(!data.id){
        return new Promise((resolve, reject) => {
            resolve({ code: 2011, msg: "参数缺失" });
        });
    }
    return Ajax.ajax({
        url: `/api/faceFeature/delete`,
        method: 'post',
        data,
        timeout:20*1000,
    }).then(res => res.data);
}

// 批量上传头像
export function registAvatarBatch(data) {
    return Ajax.ajax({
        url: '/api/user/batch_upload_avatar',
        data: data,
        timeout: 60 * 60 * 1000,
        method: 'post'
    }).then(res => res.data)
}



// 佳一学生
export function getJiaYiStudents(data={}) {
    const apiUrl = "/api/student/query";
    if (data.page_num === undefined || data.page_size === undefined) {
        return new Promise((resolve, reject) => {
            resolve({ code: 2011, msg: "学生查询参数缺失" });
        });
    }

    return Ajax.ajax({
        url: apiUrl,
        data,
        method: "post",
        timeout: 20 * 1000,
    }).then(res => res.data);
}

export function updateJiaYiStudent(data) {
    return Ajax.ajax({
        url: "/api/student/update",
        data: data,
        method: "post",
        timeout:20*1000,
    }).then(res => res.data);
}